package com.cy.cykh.app.view.base
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.Toolbar
import com.cy.cykh.app.util.ActivityManagerUtil
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity

/**
 * <pre>
 * author : jinliangshan
 * e-mail : jinliangshan@chexiang.com
 * desc   :
</pre> *
 */
abstract class BaseActivity : RxAppCompatActivity() {

     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityManagerUtil.get().addActivity(this)
        if (getContentViewID() != 0) {
            setContentView(getContentViewID())
            initView()
        }
    }


    fun initToolbar(toolbar: Toolbar?, title: String) {
        toolbar?.title = title
        toolbar?.setTitleTextColor(Color.WHITE)
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

        toolbar?.setNavigationOnClickListener { _ -> finish() }
    }


    /**
     * Activity
     *
     * @param newClass
     */
    // (1)页面跳转重载
    protected fun GoActivity(newClass: Class<*>) {
        val intent = Intent(this, newClass)
        intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
        startActivity(intent)
    }

    protected fun GoActivity(newClass: Class<*>, extras: Bundle?) {
        val intent = Intent(this, newClass)
        if (null != extras) {
            intent.putExtras(extras)
        }
        intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
        startActivity(intent)
    }

    //带返回值跳转重载
    protected fun GoStartForResult(newClass: Class<*>, flag: Int) {
        val intent = Intent(this, newClass)
        intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
        startActivityForResult(intent, flag)
    }
    protected fun GostartForResult(newClass: Class<*>, requestCode: Int, bundle: Bundle?) {
        val intent = Intent(this, newClass)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
        startActivityForResult(intent, requestCode)
    }
    /**
     * 布局
     *
     * @return
     */
    protected abstract fun getContentViewID(): Int

    /**
     * 布局
     *
     * @return
     */
    protected abstract fun initView()

    @Override
     override fun onDestroy() {
        super.onDestroy()
        ActivityManagerUtil.get().removeActivity(this)
    }

}
